package ch.mueller.android.todo;

import java.util.ArrayList;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Toast;
import ch.mueller.android.todo.controller.ToDo_SQLOpenHelper;
import ch.mueller.android.todo.model.TodoItems;
import ch.mueller.android.todo.view.AddToDo;

public class ToDo extends ListActivity {

    private SQLiteDatabase database;
    private ToDo_SQLOpenHelper sqlOpenHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_to_do);
        this.sqlOpenHelper = new ToDo_SQLOpenHelper(this);
    }

    @Override
    protected void onResume() {
        super.onResume();

        database = sqlOpenHelper.getReadableDatabase();

        ArrayList<TodoItems> todoElements = new ArrayList<TodoItems>();

        Cursor cursor = database.query(TodoItems.TABLE_NAME, null, // all
                                                                   // columns
                null, null, null, null, TodoItems.TC_PRIORITY);

        while (cursor.moveToNext()) {

            TodoItems element = new TodoItems();
            element.setId(Integer.parseInt(cursor.getString(cursor.getColumnIndex(TodoItems.TC_ID))));
            element.setTitle(cursor.getString(cursor.getColumnIndex(TodoItems.TC_TITLE)));
            element.setPriority(cursor.getInt(cursor.getColumnIndex(TodoItems.TC_PRIORITY)));
            todoElements.add(element);

        }

        ArrayAdapter<TodoItems> arrayAdapter = new ArrayAdapter<TodoItems>(this, android.R.layout.simple_list_item_1,
                todoElements);

        this.getListView().setAdapter(arrayAdapter);
        this.getListView().setOnItemClickListener(new ListListener(this));
    }

    @Override
    protected void onPause() {
        database.close();
        Intent intent = new Intent("UPADTE_WIDGET");
        sendBroadcast(intent);
        super.onPause();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.to_do, menu);
        return super.onCreateOptionsMenu(menu);
    }
    
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case ch.mueller.android.todo.R.id.opt_addTodo:

            startActivity(new Intent(this, AddToDo.class));
            
            return true;

        }
        return super.onOptionsItemSelected(item);
    }
    
    private class ListListener implements OnItemClickListener{
        
        private ListActivity listActivity;

        private ListListener(ListActivity listActivity){
            this.listActivity = listActivity;
        }
        
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            TodoItems element = null;
            try {
                element = (TodoItems) listActivity.getListView().getAdapter().getItem(arg2);
                Intent intent = new Intent(listActivity, AddToDo.class);
                intent.putExtra(TodoItems.TC_ID, element.getId());
                intent.putExtra(TodoItems.TC_PRIORITY, element.getPriority());
                intent.putExtra(TodoItems.TC_TITLE, element.getTitle());
                listActivity.startActivity(intent);
            } catch (Exception e) {
                Toast.makeText(this.listActivity, "Failed", Toast.LENGTH_SHORT).show();
            
            }
                        
        }
        
    }
}
